package mo.offer_special;

import java.util.*;

public class L007 {

    public List<List<Integer>> threeSum(int[] nums) {
        int n = nums.length;
        if (n < 3) {
            return Collections.emptyList();
        }

        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();

        for (int first = 0; first < n; first++) {
            if (first > 0 && nums[first] == nums[first-1]) {
                continue;
            }

            int third = n - 1;
            int target = -nums[first];
            for (int second = first+1; second < n; second++) {
                if (second > first + 1 && nums[second] == nums[second-1]) {
                    continue;
                }
                while (second < third && nums[second] + nums[third] > target) {
                    third--;
                }
                if (second == third) {
                    break;
                }
                if (nums[second] + nums[third] == target) {
                    res.add(Arrays.asList(nums[first], nums[second], nums[third]));
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        L007 test = new L007();
        System.out.println(test.threeSum(new int[]{-1, 0, 1, 2, -1, -4}));
    }

}
